package cn.com.chec.en.controller.admin;

import cn.com.chec.en.dao.MsgInfoDao;
import cn.com.chec.en.dao.WebsiteInfoDao;
import cn.com.chec.en.model.domain.MsgInfo;
import cn.com.chec.en.model.domain.WebsiteInfo;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.WebUtils;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MsgInfoController {
	protected static final Logger logger = Logger
			.getLogger(MsgInfoController.class);

	@Resource
	private WebsiteInfoDao websiteInfoDao;

	@Resource
	private MsgInfoDao msgInfoDao;

	@RequestMapping({ "/admin/msgInfo/list.do" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/msgInfo/msgInfoList");
		int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
		int msgFrom = WebUtils.getRequestParameterAsInt(request, "msgFrom", 0);
		int msgTo = WebUtils.getRequestParameterAsInt(request, "msgTo", 0);
		int msgStatus = WebUtils.getRequestParameterAsInt(request, "msgStatus",
				0);
		int isRead = WebUtils.getRequestParameterAsInt(request, "isRead", -1);
		int isUsed = WebUtils.getRequestParameterAsInt(request, "isUsed", -1);

		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		MsgInfo query = new MsgInfo();
		query.setMsgFrom(msgFrom);
		query.setMsgTo(msgTo);
		query.setMsgStatus(msgStatus);
		query.setIsRead(isRead);
		query.setIsUsed(isUsed);

		List<MsgInfo> msgInfoList = this.msgInfoDao.getSubList(query,
				pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.msgInfoDao.getCount(query));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		map.put("msgInfoList", msgInfoList);
		map.put("pageBean", pageBean);
		map.put("query", query);
		map.put("websiteInfoList", websiteInfoList);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/msgInfo/del.do" })
	public ModelAndView del(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String msgId = WebUtils.getRequestParameterAsString(request, "msgId");
		if ((StringUtils.isBlank(msgId))
				|| (StringUtils.split(msgId, ",").length == 0)) {
			map.put("message", "请选择需要删除的消息内容!");
			mav.addAllObjects(map);
			return mav;
		}
		try {
			for (String id : StringUtils.split(msgId, ",")) {
				this.msgInfoDao.del(id);
			}
			map.put("message", "消息删除成功!");
		} catch (Exception e) {
			map.put("message", "消息删除失败!");
			logger.error("delMessage error" + msgId);
		}
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/msgInfo/checkUnReadMsg.do" })
	public void checkUnReadMsg(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		int msgTo = 0;
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		for (WebsiteInfo website : websiteInfoList) {
			if (website.getIsSelf() == 1) {
				msgTo = website.getId();
			}
		}

		MsgInfo query = new MsgInfo();
		query.setMsgFrom(0);
		query.setMsgTo(msgTo);
		query.setMsgStatus(0);
		query.setIsRead(0);
		query.setIsUsed(-1);

		int unReadMsgCount = this.msgInfoDao.getCount(query);

		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		out.print(unReadMsgCount);
		out.flush();
		out.close();
	}

	@RequestMapping({ "/admin/msgInfo/msgInfoDetail.do" })
	public ModelAndView msgInfoDetail(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/msgInfo/msgInfoDetail");
		String msgId = WebUtils.getRequestParameterAsString(request, "msgId");
		if (StringUtils.isBlank(msgId)) {
			mav = new ModelAndView("admin/common/message");
			map.put("message", "请选择需要查看的消息内容!");
			mav.addAllObjects(map);
			return mav;
		}

		this.msgInfoDao.isAlwaysRead(msgId);

		MsgInfo msgInfo = this.msgInfoDao.getMsgInfoByMsgId(msgId);
		map.put("msgInfo", msgInfo);
		mav.addAllObjects(map);
		return mav;
	}
}